#!/bin/bash

# 获取项目根目录
project_root=$(pwd)

# 存储后台进程PID的数组
declare -a pids=()

# 清空或创建PID文件
> "$project_root/dev_pids"

# 启动api-gate
(
  cd "$project_root"
  nohup api-gate ./gate.yaml >/dev/null 2>&1 &
  echo "api-gate:pid:($!)"
  echo $! >> "$project_root/dev_pids"
  pids+=($!)
)

# 启动log_probe
(
    cd "$project_root/log_probe"
    export LOG_PROBE_DUMBO_LOG_LEVEL=Debug
    nohup cargo run -- temp.log >/dev/null 2>&1 &
    echo "log_probe:pid:($!)"
    echo $! >> "$project_root/dev_pids"
    pids+=($!)
)

# 启动log_common_service
(
    cd "$project_root/log_common_service"
    export LOG_COMMON_SERVICE_DUMBO_LOG_LEVEL=Debug
    nohup cargo run >/dev/null 2>&1 &
    echo "log_common_service:pid:($!)"
    echo $! >> "$project_root/dev_pids"
    pids+=($!)
)

# 启动log_front
(
    cd "$project_root/log_front"
    nohup npm run dev >/dev/null 2>&1 &
    echo "log_front:pid:($!)"
    echo $! >> "$project_root/dev_pids"
    pids+=($!)
)

# 在浏览器中打开http://localhost:5173
sleep 5  # 等待服务启动
open http://localhost:5173 2>/dev/null || xdg-open http://localhost:5173 2>/dev/null || echo "请手动打开浏览器访问 http://localhost:5173"

wait
